/**
* This is about <code>org.shoebox.guacamole.core.RenderTask</code>.
* {@link www.hyperfiction.fr}
* @author shoe[box]
*/

package org.shoebox.guacamole.core {

	import org.shoebox.guacamole.materials.TextureRegion;
	import com.adobe.utils.AGALMiniAssembler;
	import org.shoebox.guacamole.materials.BitmapMaterial;
	import org.shoebox.guacamole.materials.abstracts.AMaterial;
	import org.shoebox.guacamole.view.GuaScene;
	import org.shoebox.utils.logger.Logger;
	import flash.display3D.Context3DProgramType;
	import flash.geom.Matrix3D;
	
	/**
	* org.shoebox.guacamole.core.RenderTask
	* @author shoebox
	*/
	public class RenderTask {
		
		public var agalVertices		: AGALMiniAssembler;
		public var oScene				: GuaScene;
		public var oCam				: Matrix3D;
		
		protected var _prevMat			: AMaterial;
		
		protected static const vFragmentShader : Vector.<String> = Vector.<String>( [ 
																								'm44 op, va0, vc0',	// pos to clipspace
																								'mov v0, va1' 		// copy uv					
																							] );
		
		// -------o constructor
		
			/**
			* Constructor of the RenderTask class
			*
			* @public
			* @return	void
			*/
			public function RenderTask( oScene : GuaScene, oCam : Matrix3D ) : void {
			
				//
					this.oScene 	= oScene;
					this.oCam 		= oCam;
					
				//
					agalVertices = new AGALMiniAssembler( );
					agalVertices.assemble( Context3DProgramType.VERTEX , vFragmentShader.join('\n') );
				
			}

		// -------o public
			
			/**
			* addMaterial function
			* @public
			* @param 
			* @return
			*/
			final public function addMaterial( mat : AMaterial ) : void {
				
				if( mat is TextureRegion ){
					
					if( _prevMat !== ( mat as TextureRegion ).refAtlas ){
						_prevMat = ( mat as TextureRegion ).refAtlas;
						oScene.setTextureAt( 0 ,  ( mat as TextureRegion ).refAtlas.tTexture );
					}
					
				}else if( mat is BitmapMaterial ){
					if( _prevMat !== mat ){
						oScene.setTextureAt( 0 , ( mat as BitmapMaterial ).tTexture );
						_prevMat = mat;
					}
				}else{
					_prevMat = null;
					oScene.setTextureAt( 0 , null );
				}
				
			}
			
		// -------o protected

		// -------o misc

			public static function trc(...args : *) : void {
				Logger.log(RenderTask, args);
			}
	}
}
